package com.mx.adapt.anytype

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.mx.adapt.MXBaseSimpleAdapt
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.atomic.AtomicLong

abstract class MXBaseTypeAdapt<T>(list: ArrayList<T> = arrayListOf()) :
    MXBaseSimpleAdapt<T>(list) {
    private val currentViewType by lazy { viewTypeIndex.incrementAndGet() }
    private val currentAdaptIndex by lazy { adaptIndex.incrementAndGet() }

    /**
     * 获取每行显示的Item个数
     */
    open fun getSpanSize(): Int = 1

    final override fun getItemViewType(position: Int): Int {
        return currentViewType
    }

    final override fun createItem(
        inflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): ViewBinding {
        return createItem(inflater, parent)
    }

    abstract fun createItem(inflater: LayoutInflater, parent: ViewGroup): ViewBinding

    fun getAdaptViewType() = currentViewType
    fun getAdaptIndex() = currentAdaptIndex

    companion object {
        private val viewTypeIndex = AtomicInteger()
        private val adaptIndex = AtomicLong()
    }
}